local FZutil=require("app.FZutil")

local Alert={
    TYPE_OK=1,
    TYPE_YES_NO=2,
}
Alert.curr_aler_uimap = nil

local AlertUI=class("AlertUI",FZutil.createUILayer)

function AlertUI:ctor(title,msg,type,callback,force)
	local uimap=FZutil.createNoneOutLineNode("GeneralUIall/AlertNode")
	self:addChild(uimap.root)
	self.uimap=uimap
	Alert.curr_aler_uimap=uimap
	uimap.root:setPosition(display.center)
	
	uimap.Text_title:setString(title)
	uimap.Text_msg:setString(msg)
    if type==Alert.TYPE_YES_NO then
		uimap.Button_OK:hide()
		uimap.Text_OK:hide()
		uimap.Node_yesno:show()
		uimap.Button_YES:onTouch(function(e)
		  if e.name=="ended" then
		  	   self:close(function()
                    if callback then
                        callback(true)
                    end
		  	   end)
		  end
		end)
		uimap.Button_NO:onTouch(function(e)
		  if e.name=="ended" then
		  	   self:close(function()
                    if callback then
                        callback(false)
                    end
		  	   end)
		  end
		end)
	else
        uimap.Node_yesno:hide()
        uimap.Button_OK:show()
        uimap.Button_OK:onTouch(function(e)
            if e.name=="ended" then
                self:close(callback)
            end
        end)
        uimap.Text_OK:show()
	end
	--6f811b
	local outlienc1=cc.c4b(111,129,27,255)
	--81381b
	local outlinec2=cc.c4b(0x81,0x38,0x1b,255)
	uimap.Text_OK:enableOutline(outlienc1,1)
	uimap.Text_YES:enableOutline(outlienc1,1)
	uimap.Text_NO:enableOutline(outlinec2,1)
	if not force then 
		uimap.root:setScale(0)
		FZutil.windowShowEff(uimap.root)
	end
	self.force=force
end


function AlertUI:close(close_callback)
	Alert.curr_aler_uimap=nil
	if self.force then 
		close_callback()
		self:removeSelf()
	else
    	FZutil.windowEffCloseToRemove(self.uimap.root,self,close_callback)
    end
end

function AlertUI:setRichMsg(msgs)
	local uimap=self.uimap
	uimap.Text_msg:hide()
	local rich=ccui.RichText:create()
	uimap.root:addChild(rich)
	rich:setSize(373,93)
	rich:ignoreContentAdaptWithSize(false)
	for i, ele in ipairs(msgs) do
	   local element
	   if type(ele)=="string" then
	   	   element=ccui.RichElementText:create(i,cc.c3b(0xff,0xff,0xff),0xff,ele,Define.DEFAULT_FONT,22)
	   else
	       element=ccui.RichElementCustomNode:create(i,cc.c3b(0xff,0xff,0xff),0xff,ele)
	   end
		rich:pushBackElement(element)
	end
	rich:setPosition(0,5)
end

function AlertUI:setBtnNames( type , names )
	local uimap=self.uimap
	if type == Alert.TYPE_YES_NO then 
		uimap.Text_YES:setString(names[1])
		uimap.Text_NO:setString(names[2])
	else
		uimap.Text_OK:setString(names[1])
	end
end

---------------上面是封装的UI------------

--
function Alert:show(title,msg,type,callback,btnNames)
    local at=AlertUI.new(title,msg,type,callback)
    if btnNames then 
    	at:setBtnNames(type,btnNames)
    end
    display.getRunningScene():addChild(at)
    return at
end

function Alert:showForce(title,msg,type,callback)
	print("Alert showForce")
	local at=AlertUI.new(title,msg,type,callback,true)
    display.getRunningScene():addChild(at,100000)
    return at
end

--
function Alert:showRich(title,msgs,type,callback)
    local at=AlertUI.new(title,"",type,callback)
    at:setRichMsg(msgs)
    display.getRunningScene():addChild(at)
end

return Alert